package com.qyy.remotemonitor;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.qyy.remotemonitor.common.ui.BaseNoPresenterActivity;
import com.qyy.remotemonitor.common.util.LocationManagerUtil;
import com.qyy.remotemonitor.ui.service.RemoteMonitorService;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

/**
 * Created by qinyy on 2018/9/28.
 */

public class MainActivity extends BaseNoPresenterActivity
{

    @Override
    protected int getLayoutResource()
    {
        return R.layout.act_main;
    }

    @Override
    protected void initTodo(Bundle savedInstanceState)
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            requestPermissions();
        }

        // 启动服务
        Intent intent1 = new Intent();
        intent1.setClass(getApplicationContext(),RemoteMonitorService.class);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            // 8.0 以上不允许后台service使用startService 启动服务
            startForegroundService(intent1);
        }
        else
        {
            startService(intent1);
        }
        Intent intent2 = new Intent();
        intent2.setClass(getApplicationContext(),MyJobService.class);
        startService(intent2);

        finish();
    }



    private void requestPermissions() {
        final RxPermissions rxPermission = new RxPermissions(MainActivity.this);
        rxPermission.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean granted) throws Exception
            {
                rxPermission
                        .requestEach(Manifest.permission.INTERNET,
                                Manifest.permission.ACCESS_NETWORK_STATE,
                                Manifest.permission.ACCESS_WIFI_STATE,
                                Manifest.permission.ACCESS_COARSE_LOCATION,
                                Manifest.permission.CHANGE_WIFI_STATE,
                                Manifest.permission.WAKE_LOCK,
                                Manifest.permission.RECEIVE_BOOT_COMPLETED,
                                Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION,
                                Manifest.permission.READ_LOGS,
                                Manifest.permission.CALL_PHONE,
                                Manifest.permission.READ_CALL_LOG)
                        .subscribe(new Consumer<Permission>() {
                            @Override
                            public void accept(Permission permission) throws Exception {
                                if (permission.granted)
                                {
                                    // 用户已经同意该权限
                                    LogUtils.d( permission.name + " is granted.");
                                    if(permission.name.equals(Manifest.permission.ACCESS_FINE_LOCATION))
                                    {
                                        LocationManagerUtil.getInstance(getApplication()).initLocation();
                                    }

                                }
                                else if (permission.shouldShowRequestPermissionRationale) {
                                    if(permission.name.equals(Manifest.permission.SYSTEM_ALERT_WINDOW)
                                            || permission.name.equals(Manifest.permission.READ_LOGS))
                                        return;
                                    // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                                    LogUtils.d( permission.name + " is denied. More info should be provided.");
                                    Toast.makeText(MainActivity.this,"您拒绝了授予" + permission.name + "权限，如影响正常使用，请到系统设置中手动授予对应权限！",Toast.LENGTH_LONG).show();
                                }
                                else {
                                    if(permission.name.equals(Manifest.permission.SYSTEM_ALERT_WINDOW)
                                            || permission.name.equals(Manifest.permission.READ_LOGS))
                                        return;
                                    // 用户拒绝了该权限，并且选中『不再询问』
                                    LogUtils.d( permission.name + " is denied.");
                                    Toast.makeText(MainActivity.this,"您拒绝了授予" + permission.name + "权限，如影响正常使用，请到系统设置中手动授予对应权限！",Toast.LENGTH_LONG).show();
                                }
                            }
                        });

            }
        });

    }

}
